#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "boundobj.h"
#include "Echarts/cwidgetbrowser.h"

#include <QTimer>
#include <QApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_pBrowser = new CWidgetBrowser(this);

    ui->Layout_Echarts->addWidget(m_pBrowser);

    BoundOBJ *obj = new BoundOBJ(this);
    m_pBrowser->SetWindowObject(obj);


    QString path = QApplication::applicationDirPath();

    QString file = path + "/../Charts/echarts/1.html";
    // m_pBrowser->LoadUrl("https://www.baidu.com/");
    m_pBrowser->LoadUrl(file);

//    QTimer *pTimer = new QTimer(this);
//    connect(pTimer, &QTimer::timeout, [&](){
//        static int i = 0;

//        m_pBrowser->InvokeScript(QString("setGaugeValue(%1);").arg(i));

//        static bool isAdd = true;
//        if (i == 100){
//            isAdd = false;
//        }
//        else if (i == 0) {
//            isAdd = true;
//        }
//        isAdd ? ++i : --i;
//    });
//    pTimer->start(100);

    QEventLoop *loop = new QEventLoop;
    QTimer::singleShot(5000, [loop, this]()
    {
        loop->quit();
        loop->deleteLater();
        m_pBrowser->InvokeScript(QString("setGaugeValue(100);"));
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

